package com.scy.java.io.buffered;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

//自定义比较的规则，实现文本排序赋值
public class TextSortCopyList {
    public static void main(String[] args) {
        //定义一个缓冲字符流管道与源文件接通
        try {
            BufferedReader br =
                    new BufferedReader(
                            new FileReader("d:/出师表2.txt"));
            //定义一个缓冲字符输出流管道与目标文件接通
            BufferedWriter bw =
                    new BufferedWriter(
                            new FileWriter("d:/Copy_出师表2.txt"));
            //定义一个list集合存储数据
            List<String> dataList = new ArrayList<>();
            String line;
            while ((line = br.readLine()) != null){
                //定义数据存储到集合中
                dataList.add(line);
            }
            //定义list集合存储首字符
            List<Object> sizes = new ArrayList<>();
            Collections.addAll(sizes,
                    "一","二","三","肆","五","陆","柒","八","玖");
            //自定义截取规则， “.”之前截取比较sizes中的中文字符
            Collections.sort(dataList,
                    (o1,o2)->sizes.indexOf(o1.substring(0,o1.indexOf(".")))
                        -sizes.indexOf(o2.substring(0,o2.indexOf("."))));
            //遍历集合中的每个元素,用字符输出流管道写出并换行
            for (String dataChar: dataList) {
                //用缓冲输出流写出数据并换行
                bw.write(dataChar);
                bw.newLine();
                //强制行刷新
                bw.flush();
            }
            System.out.println("复制完毕");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
